{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "97301606-444c-4390-bc7b-50d245f4f2a2",
   "metadata": {},
   "source": [
    "## list to series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "93a0d52f-c965-4af9-abfa-24e5df8ab739",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "99fe3e1d-3a36-4aa1-874c-7fcbffc8a2e5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['X1', 'X2', 'X3', 'X4']\n",
      "X1\n"
     ]
    }
   ],
   "source": [
    "list = ['X1','X2','X3','X4']\n",
    "print(list)\n",
    "print(list[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "ac6aebb1-7f2a-4be5-948a-dec3a92a5118",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    X1\n",
      "1    X2\n",
      "2    X3\n",
      "3    X4\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "myseries = pd.Series(list)\n",
    "print(myseries)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "55ca784f-b7e9-4d6b-8263-bc8b681a2f4d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a    X1\n",
      "b    X2\n",
      "c    X3\n",
      "d    X4\n",
      "dtype: object\n"
     ]
    }
   ],
   "source": [
    "myseries = pd.Series(list,index = ['a','b','c','d'])\n",
    "print(myseries)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ac367aed-4ac1-4cb2-8916-9ca4e3345215",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "09dab40f-1687-4321-bf52-32eb96243a36",
   "metadata": {},
   "source": [
    "## Series to dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "09ac940a-b04e-46f8-ac8e-3afcbb7d8012",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3, 4], ['X1', 'X2', 'X3', 'X4'], [85, 88, 90, 95]]\n"
     ]
    }
   ],
   "source": [
    "list_a = [1,2,3,4]\n",
    "list_b = ['X1','X2','X3','X4']\n",
    "list_c = [85,88,90,95]\n",
    "\n",
    "# s_a = pd.Series(list_a)\n",
    "# s_b = pd.Series(list_b)\n",
    "# s_c = pd.Series(list_c)\n",
    "\n",
    "pd_data = [list_a,list_b,list_c]\n",
    "print(pd_data)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "951abff0-b36f-4524-aba5-0bf43dad8cbd",
   "metadata": {},
   "source": [
    "### 二维数组转dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5e2c9034-6fb1-4cfb-a031-a72393a711cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    0   1   2   3\n",
      "0   1   2   3   4\n",
      "1  X1  X2  X3  X4\n",
      "2  85  88  90  95\n"
     ]
    }
   ],
   "source": [
    "df1 = pd.DataFrame(pd_data)\n",
    "print(df1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0662939-9929-4fb0-8dad-582bae371c34",
   "metadata": {},
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d2c1c543-c5ae-41b5-b7bc-b33787ccada1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0   1   2\n",
      "0  1  X1  85\n",
      "1  2  X2  88\n",
      "2  3  X3  90\n",
      "3  4  X4  95\n"
     ]
    }
   ],
   "source": [
    "df2 = df1.T\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d074c7b3-c295-4547-8ed6-f7c65dbc8f92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  编号  姓名  成绩\n",
      "0  1  X1  85\n",
      "1  2  X2  88\n",
      "2  3  X3  90\n",
      "3  4  X4  95\n"
     ]
    }
   ],
   "source": [
    "df2.columns = ['编号','姓名','成绩']\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a9eb0ae5-4d85-4fec-a348-ebd5a769b9af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "  编号  姓名  成绩\n",
      "0  1  X1  85\n",
      "1  2  X2  88\n",
      "2  3  X3  90\n",
      "3  4  X4  95\n"
     ]
    }
   ],
   "source": [
    "df2.set_index('编号',drop=True)\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "9d8287a7-cc55-4b4c-bedc-22ae76fbe78b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    姓名  成绩\n",
      "编号        \n",
      "1   X1  85\n",
      "2   X2  88\n",
      "3   X3  90\n",
      "4   X4  95\n"
     ]
    }
   ],
   "source": [
    "df2.set_index('编号',drop=True,inplace = True)\n",
    "print(df2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d404d11f-20f6-4b76-a659-e83ed502c4f4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8dda59fe-f2f1-48c1-b859-ef93e28fb60c",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2.to_csv('data/pd_data.csv',index=None,header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfaa2d85-05a6-40c3-a20d-400fd36b459c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
